//index.js
//获取应用实例
const app = getApp()
const utils = require('../../utils/util.js')

Page({
  data:{
    seviecaccount:'',
    userlocal:false,
    userid:'',
    integral:"",
    servierInfo:false,
    audiobool:false,
    imgurl:'',
    userphone:false
  },
  onLoad:function(options){
  },
  onShow:function(){
    // 获取用户信息
      var that = this;
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {//判断是否授权，已经授权
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            //this.getUserInfo();//获取用户信息
            wx.getUserInfo({
              success: function (res) {
                that.setData({
                  userlocal: true,
                  userid: wx.getStorageSync("nack_id"),
                  imgurl: res.userInfo.avatarUrl,
                })
              },
            })
            wx.login({
              success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                utils.getData('user/getSessionKeyAndOpenid', { code: res.code }, true).then(function (res_login) {
                  wx.setStorageSync("nack_id", res_login.data.nack_id)
                  wx.setStorageSync("openId", res_login.data.openid);//openid
                  wx.setStorageSync("session_key", res_login.data.session_key);//回话密钥               
                })
              }
            });
            this.getAjax()
          } else {
            wx.clearStorage()
            // 登录
            wx.login({
              success: res => {
                console.log(res)
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                utils.getData('user/getSessionKeyAndOpenid', { code: res.code }, true).then(function (res_login) {
                  wx.setStorageSync("nack_id", res_login.data.nack_id)
                  wx.setStorageSync("openId", res_login.data.openid);//openid
                  wx.setStorageSync("session_key", res_login.data.session_key);//回话密钥               
                })
              }
            });
            //this.getUserInfo();//获取用户信息
          }
        }
      })
    // var  userbool;
    // if (wx.getStorageSync("userphone")) {
    //   console.log('有手机号')
    //   userbool =false;
    // } else {
    //   console.log('没有手机号')
    //   userbool = true;
    // }
    // this.setData({
    //   userphone: userbool
    // })
  },
  getAjax: function () {
    var that =this;
    utils.getData('user/indexInfo', { open_id: wx.getStorageSync("openId") }, true).then(function (res) {
      // console.log(res.data)
      var userbool;
      if (res.data.data.phone) {  //有手机号
        userbool = false;
        wx.setStorageSync("userphone", res.data.data.phone)
      } else {
        userbool = true;
      }
      that.setData({
        userphone: userbool
      })
      if (res.data.data.customer_num) {
        wx.setStorageSync("customernum", res.data.data.customer_num)
        wx.setStorageSync("customerid", res.data.data.belong_coutomer)
        var serbool =true
      }else{
        var serbool = false
      }
      that.setData({
        integral: res.data.data.score,
        userid: res.data.data.nack_id,
        seviecaccount:res.data.data.customer_num,
        servierInfo: serbool,
        // imgurl: res.userInfo.avatarUrl,
      })
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    wx.setStorageSync("userInfo", e.detail.userInfo);
    // console.log(e)
    this.getAjax()
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      userlocal: true,
      imgurl: e.detail.userInfo.avatarUrl,
    })
  },
  perfectmess:function(){
    console.log('跳转完善页面')
    wx.navigateTo({
      url: '../success/success?form=1',//跳转完善页面，完成后继续跳到首页
    })
  },
  shuaxin:function(){
    console.log('刷新积分')
    this.getAjax()
  },
  audioshow:function(){  //点击视频显示  
    this.setData({
      audiobool: true
    })
  },
  integralShow:function(){  //积分兑换说明
    console.log('跳转积分兑换说明')
    wx.navigateTo({
      url: '../integral_specification/integral_specification'
    })
  },
  integralType: function () {  //积分兑换种类
    console.log('跳转积分兑换种类')
    wx.navigateTo({
      url: '../integral_list/integral_list'
    })
  },
  contactUs: function () {  //联系我们contact
    console.log('跳转联系我们');
    wx.navigateTo({
      url: '../contact/contact'
    })
  },
  intergraJilu(){
    wx.navigateTo({
      url:"../jilu/jilu"
    })
  },
  serviceWx(){
    wx.navigateBack({
      delta: -1,
      success(){
        console.log(123)
      },
      fail(){
        console.log(312)
      }
    })
    if (this.data.seviecaccount){
      wx.showModal({
        title: "专属微信客服",
        content: "请您直接联系您的专属客服（微信号:" + this.data.seviecaccount + "）确认您的兑换"
      })
    }
  },
  jianjie(){
    wx.navigateTo({
      url: '../jianjie/jianjie',
    })
  }
})
